function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var t = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var i = arguments[t];
        for (var n in i) Object.prototype.hasOwnProperty.call(i, n) && (e[n] = i[n]);
    }
    return e;
}, i = e(require("../../utils/toptip.js")), n = e(require("../../utils/util.js")), o = e(require("../../utils/api-code.js")), r = require("../../utils/request.js"), s = require("../../utils/host.js"), u = require("../../utils/user.js"), a = e(require("../../components/mobile-validator/mobile-validator")), c = {
    weapp: {
        bindingWxUser: function(e) {
            return (0, r.post)(s.apiPrefix + "/weapp/bindingwxuser", e);
        }
    }
};

Page({
    data: {
        submitting: !1,
        quickLoginMode: wx.getSystemInfoSync().SDKVersion >= "1.2.0",
        $$mv: {}
    },
    onLoad: function(e) {
        var t = this;
        this.query = e, (0, u.getTokens)().then(function(e) {
            t.loginCode = "", wx.showLoading({
                title: "加载中..."
            }), wx.login({
                success: function(e) {
                    return t.loginCode = e.code;
                },
                complete: wx.hideLoading
            }), setInterval(function() {
                wx.login({
                    success: function(e) {
                        return t.loginCode = e.code;
                    }
                });
            }, 27e4);
        }), a.default.addTo(this, "$$mv");
    },
    bindingWxUser: function(e) {
        var n = this;
        return this.setData({
            submitting: !0
        }), (0, u.getTokens)().then(function(r) {
            c.weapp.bindingWxUser(t({}, r, n.query, e)).then(function(e) {
                e.code === o.default.SUCCESS ? n.onBindingSuccess(e.data) : i.default.show(n, e.message);
            });
        }).catch(function(e) {
            return console.error(e);
        }).then(function() {
            n.setData({
                submitting: !1
            });
        });
    },
    onBindingSuccess: function(e) {
        (0, u.updateTokens)(e), wx.showToast({
            title: "已登录"
        }), setTimeout(this.reloadPrevPage, 1500);
    },
    reloadPrevPage: function() {
        var e = getCurrentPages(), t = e[e.length - 2];
        if ("function" == typeof t.onMobileVerify) t.onMobileVerify(), wx.navigateBack(); else {
            var i = "/" + (t.route || __wxRoute) + "?" + n.default.serializeParams(t.__query);
            e.length <= 2 && wx.reLaunch ? wx.reLaunch({
                url: i
            }) : wx.navigateBack({
                success: function() {
                    wx.redirectTo({
                        url: i
                    });
                }
            });
        }
    },
    validateForm: function(e) {
        var t = e.mobile, o = e.verifycode;
        return t ? n.default.isMobile(t) ? !!o || (i.default.show(this, "请输入验证码"), !1) : (i.default.show(this, "手机号格式不正确"), 
        !1) : (i.default.show(this, "请输入手机号"), !1);
    },
    submit: function() {
        this.validateForm(this.data.$$mv.form) && this.bindingWxUser(t({}, this.data.$$mv.form));
    },
    toManualMode: function() {
        this.setData({
            quickLoginMode: !1
        });
    },
    quickLogin: function(e) {
        var t = e.detail, n = t.errMsg, o = t.iv, r = t.encryptedData;
        "getPhoneNumber:fail user deny" === n || (n.includes("getPhoneNumber:fail") ? (i.default.show(this, "快速获取失败，请手动输入"), 
        this.setData({
            quickLoginMode: !1
        })) : this.bindingWxUser({
            encryptedData: r,
            iv: o,
            code: this.loginCode
        }));
    }
});